package com.wgke.bluetooth.ui;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;

import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;

import com.wgke.bluetooth.R;
import com.wgke.bluetooth.tools.BtMsg;
import com.wgke.bluetooth.utils.SpUtil;
import com.wgke.bluetooth.utils.ToastUtil;
import com.wgke.bluetooth.vm.PolicyVM;

import java.lang.reflect.Array;
import java.util.Arrays;

public class PolicyActivity extends BaseActivity<PolicyVM> {
    private EditText etHeader, etFooter;
    private Switch open, size, sum;

    @Override
    public int layoutId() {
        return R.layout.activity_policy;
    }

    @Override
    protected void initStateBar() {
        super.initStateBar();

    }

    @Override
    public void initView() {

        open = findViewById(R.id.swOpen);
        size = findViewById(R.id.swOpen4);
        sum = findViewById(R.id.swOpen5);
        etHeader = findViewById(R.id.et2);
        etFooter = findViewById(R.id.et3);

        findViewById(R.id.ivMore).setOnClickListener(v -> {
            showPopupMenu(v);
        });
        findViewById(R.id.policySave).setOnClickListener(v -> {
            savePolicy(open.isChecked(), etHeader.getText().toString(), etFooter.getText().toString(), size.isChecked(), sum.isChecked());
        });
        vm.isShowLoading.observe(this, aBoolean -> findViewById(R.id.loading_layout).setVisibility(aBoolean ? View.VISIBLE : View.INVISIBLE));
        loadPolicy();

        Switch parse = findViewById(R.id.swOpen6);
        parse.setChecked(SpUtil.getUseParse());
        parse.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SpUtil.setUseParse(isChecked);
        });
    }

    public void loadPolicy() {
        vm.isShowLoading.postValue(false);
        open.setChecked(SpUtil.getUsePolicy());
        findViewById(R.id.clContent).setVisibility(SpUtil.getUsePolicy() ? View.VISIBLE : View.INVISIBLE);
        open.setOnCheckedChangeListener((buttonView, isChecked) -> findViewById(R.id.clContent).setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE));

        byte[] x = SpUtil.getUsePolicyHeader();
        if (x != null && x.length == 2) {
            Log.e("PolicyActivity", "header:" + Arrays.toString(x));
            etHeader.setText(BtMsg.byteArrayToHexString(x));
        }

        int y = SpUtil.getUsePolicyFooter();
        Log.e("PolicyActivity", "footer:" + y);
        if (y != 0) {
            etFooter.setText(BtMsg.byteArrayToHexString(new byte[]{(byte) y}));
        }
        size.setChecked(SpUtil.getUsePolicySize());
        sum.setChecked(SpUtil.getUsePolicySum());
    }

    public void savePolicy(boolean isPolicy, String header, String footer, boolean isSize, boolean isSum) {
        Log.e("PolicyActivity", "savePolicy: " + isPolicy + "  " + header + "  " + footer + "  " + isSize + "  " + isSum);
        try {
            byte[] headerBytes = BtMsg.hexStringToByteArray(header);
            if (headerBytes != null && headerBytes.length == 2) {
                SpUtil.setUsePolicyHeader(headerBytes[0], headerBytes[1]);
            } else if (isPolicy) {
                ToastUtil.showMsg("请输入16进制数据，头部数据 例如 5A A5");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            byte[] bytes = BtMsg.hexStringToByteArray(footer);
            if (bytes != null && bytes.length == 1) {
                SpUtil.setUsePolicyFooter(bytes[0]);
            } else if (isPolicy) {
                ToastUtil.showMsg("请输入16进制数据，尾部数据 例如 AA" + footer);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        SpUtil.setUsePolicy(isPolicy);
        SpUtil.setUsePolicySize(isSize);
        SpUtil.setUsePolicySum(isSum);

        vm.isShowLoading.postValue(true);

        postDelayed(1000, () -> {
            ToastUtil.showMsg("保存成功");
            vm.isShowLoading.postValue(false);
        });


    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public boolean isUseEventBus() {
        return false;
    }

    public void importDefaultData() {
        SpUtil.setUsePolicy(false);
        SpUtil.setUsePolicyHeader((byte) 0x5A, (byte) 0xA5);
        SpUtil.setUsePolicyFooter((byte) 0xAA);
        SpUtil.setUsePolicySize(true);
        SpUtil.setUsePolicySum(true);
        vm.isShowLoading.postValue(true);
        postDelayed(1000, this::loadPolicy);
    }

    @SuppressLint("NonConstantResourceId")
    private void showPopupMenu(View view) {
        PopupMenu popup = new PopupMenu(this, view);
        popup.getMenuInflater().inflate(R.menu.toolbar_menu2, popup.getMenu());
        popup.setOnMenuItemClickListener(item -> {
            if (R.id.action_import_default2 == item.getItemId()) {
                importDefaultData();
                return true;
            }
            return false;
        });
        popup.show();
    }
}
